
<?php

//initialisation

$defaultCategory=getPortletPreference("ParentCategory");

if (isset($defaultCategory)) $defaultCategory="parentCategoryId=".$defaultCategory;
else $defaultCategory="parentCategoryId is NULL";



if (getParameter("category")) $parentCategory=getObjByKey("shop_category","id","category",getParameter("category"),"portalId",$portalId); else  {

    $parentCategory=getObjByKey("shop_category","id","parentCategoryId",getPortletPreference("ParentCategory"));

}
	
	?>
	
	<div class="shop_filter border">
        <b>Catégories</b>
        <hr/>
	<?php




        function isChild($id,$selected)
        {

            if (getObjByKey("shop_category","parentCategoryId","id",$id)==$selected)  return true;

            return false;
        }

        function isParent($id,$selected)
        {
          // if ($id==$selected) return true;
           //    if (getObjByKey("shop_category","parentCategoryId","id",$selected)==$id)  return true;

            $sql="SELECT T2.id
FROM (
    SELECT
        @r AS _id,
        (SELECT @r := parentCategoryId FROM shop_category WHERE id = _id) AS parent_id,
        @l := @l + 1 AS lvl
    FROM
        (SELECT @r := $selected, @l := 0) vars,
        shop_category h
    WHERE @r <> 0) T1
JOIN shop_category T2
ON T1._id = T2.id
ORDER BY T1.lvl DESC";
            $rs = ew_LoadRecordset($sql);

            while(!$rs->EOF){

              if ($rs->fields["id"]==$id) return true;

                $rs->MoveNext();
            }

            return false;
        }


        function hasChild($parent_id)
        {
            $sql = "SELECT COUNT(*) as count FROM shop_category WHERE parentCategoryId = ' " . $parent_id . " ' ";
            $rs = ew_LoadRecordset($sql);
            return $rs->fields["count"];
        }

        function CategoryTree($list,$parent,$append,$selected)
        {
            global $visibleCategories;
            $count=countProducts($parent->fields["id"]); // count allsiblings
            $list = '<li id="cat_'.$parent->fields["id"].'"><a href="'.createNiceUrl("pageName","shop","",$parent->fields["category"]).'" ><span>'.$parent->fields["lib_FR"].' ('.$count.')</span></a>';


            $list .="</li><script>$('#cat_".$parent->fields["id"]."').hide();</script>";
            //$selected==$parent->fields["id"] || getObjByKey("shop_category","parentCategoryId","id",$selected)==$parent->fields["id"]
            if (isParent($parent->fields["id"],$selected) || isChild($parent->fields["id"],$selected)) {

                $list .=" <script>$('#cat_".$parent->fields["id"]."').show();</script>";

                array_push($visibleCategories,$parent->fields["id"]);
            }
            if (hasChild($parent->fields["id"])) // check if the id has a child
            {
                $append++; // this is our basis on what level is the category e.g. (child1,child2,child3)
                $list .= "<ul class='child child".$append." '>";
                $sql = "SELECT * FROM shop_category WHERE parentCategoryId = ' " . $parent->fields["id"] . " ' ";
                $rs = ew_LoadRecordset($sql);

                while(!$rs->EOF){

                    $list .= CategoryTree($list,$rs,$append,$selected);


                 //  $list .=" <script>$('#cat_".$rs->fields["id"]."').hide();</script>";
                    $rs->MoveNext();
                }
                $list .= "</ul>";
            }
            return $list;
        }
        function CategoryList($parentCategory,$selected)
        {
            global $portalId,$visibleCategories;
            $list = "";

            $sql = "SELECT * FROM shop_category WHERE $parentCategory and portalId=$portalId";
         //   echo $sql;
            $rs = ew_LoadRecordset($sql);

            $mainlist = "<ul class='parent'>";
            while(!$rs->EOF){
                $mainlist .= CategoryTree($list,$rs,$append = 0,$selected);
                $rs->MoveNext();
            };
            $mainlist .= "</ul>";
            return $mainlist;
        }
$visibleCategories=array();
echo CategoryList($defaultCategory,$parentCategory);
foreach ($visibleCategories as $id)
{
    $parentCategory=getObjByKey("shop_category","parentCategoryId","id",$id);

    $sql = "SELECT * FROM shop_category WHERE parentCategoryId=$parentCategory and portalId=$portalId";
    //   echo $sql;
    $rs = ew_LoadRecordset($sql);


    while(!$rs->EOF){

        echo "<script>$('#cat_".$rs->fields["id"]."').show();</script>";

        $rs->MoveNext();
    };

}





?>
        <b>Marque</b>
        <hr/>
        <?php $brandId=getObjByKey("shop_brands","id","lib_lt",getParameter("brand")); ?>
        <select  size="6" id="brands">
            <?php


            $sql = "SELECT * FROM shop_brands";
            //   echo $sql;
            $rs = ew_LoadRecordset($sql);


            while(!$rs->EOF){

                ?>
                <option value="<?php echo $rs->fields["lib_lt"]; ?>"><?php echo $rs->fields["lib_lt"]; ?></option>
                <?php
                $rs->MoveNext();
            };


            ?>


</select>

<script type="text/javascript">

    $("#brands").change(function () {
      /*  var str = "";
        $("select option:selected").each(function () {
            str += $(this).text() + " ";
        });
        $("div").text(str);*/
       // alert($(this).val());
    <?php $url=createNiceUrl("pageName","shop","",getParameter("category"),"***","???");

    $url=str_replace("***-","",$url);

    ?>
      var  url="<?php echo  $url ?>";
        url=url.replace("???",$(this).val());
        location.href=url;
    });

</script>



        <b>Prix</b>
        <hr/>
        <select  size="3">
       <option><100 <?php echo $app_shop_currency; ?> </option>
        <option>100 <?php echo $app_shop_currency; ?> - 500 <?php echo $app_shop_currency; ?> </option>
            <option>500 <?php echo $app_shop_currency; ?> - 1000 <?php echo $app_shop_currency; ?> </option>
            <option>1000 <?php echo $app_shop_currency; ?> - 5000 <?php echo $app_shop_currency; ?> </option>
            <option>>5000 <?php echo $app_shop_currency; ?> </option>
        </select>
</div>

